package com.jbinfo.common.utils;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import com.github.miemiedev.mybatis.paginator.domain.Order;
import com.github.miemiedev.mybatis.paginator.domain.PageBounds;
import com.github.miemiedev.mybatis.paginator.domain.PageList;
/**
 * 
* @ClassName: DataTables 
* @Description: TODO DataTables参数封装类
* @author zhanglei
* @date 2014-11-21 上午9:38:10 
* @param <T>
 */
public class DataTables<T> implements Serializable {

	private static final long serialVersionUID = -8225311114808123038L;

	// DataTables传到Action参数
	private String aoData;
	/**
	 * DataTables分页返回页面参数
	 */
	private List<T> aaData = new ArrayList<T>();
	private Long iTotalRecords = 0l;
	private Long iTotalDisplayRecords = 0l;
	private String sEcho = "sEcho";

	// DataTables分页每页行数
	private Long iDisplayLength = 10l;
	// DataTables分页 当前页起始行号 第一页为0
	private Long iDisplayStart = 0l;
	
	// ///////////////////////////////////////////////////////////////////////////////////////////
	/**
	 * 
	 * @Description:TODO
	 * @return
	 * @return String
	 * @exception:
	 * @author: 
	 * @time:2014-12-18 上午10:38:45
	 */
	public String getAoData() {
		return aoData;
	}
	/**
	 * 
	 * @Description:TODO
	 * @param aoData
	 * @return void
	 * @exception:
	 * @author: 
	 * @time:2014-12-18 上午10:38:37
	 */
	public void setAoData(String aoData) {
		this.aoData = aoData;
	}
	/**
	 * 
	 * @Description:TODO
	 * @return
	 * @return List<T>
	 * @exception:
	 * @author: 
	 * @time:2014-12-18 上午10:38:30
	 */
	public List<T> getAaData() {
		return aaData;
	}
	/**
	 * 
	 * @Description:TODO
	 * @param aaData
	 * @return void
	 * @exception:
	 * @author: 
	 * @time:2014-12-18 上午10:38:23
	 */
	public void setAaData(List<T> aaData) {
		this.aaData = aaData;
	}
	/**
	 * 
	 * @Description:TODO
	 * @return
	 * @return Long
	 * @exception:
	 * @author: 
	 * @time:2014-12-18 上午10:38:16
	 */
	public Long getiTotalRecords() {
		return iTotalRecords;
	}
	/**
	 * 
	 * @Description:TODO
	 * @param iTotalRecords
	 * @return void
	 * @exception:
	 * @author: 
	 * @time:2014-12-18 上午10:38:09
	 */
	public void setiTotalRecords(Long iTotalRecords) {
		this.iTotalRecords = iTotalRecords;
	}
	/**
	 * 
	 * @Description:TODO
	 * @return
	 * @return Long
	 * @exception:
	 * @author: 
	 * @time:2014-12-18 上午10:38:03
	 */
	public Long getiTotalDisplayRecords() {
		return iTotalDisplayRecords;
	}
	/**
	 * 
	 * @Description:TODO
	 * @param iTotalDisplayRecords
	 * @return void
	 * @exception:
	 * @author: 
	 * @time:2014-12-18 上午10:37:56
	 */
	public void setiTotalDisplayRecords(Long iTotalDisplayRecords) {
		this.iTotalDisplayRecords = iTotalDisplayRecords;
	}
	/**
	 * 
	 * @Description:TODO
	 * @return
	 * @return String
	 * @exception:
	 * @author: 
	 * @time:2014-12-18 上午10:37:50
	 */
	public String getsEcho() {
		return sEcho;
	}
	/**
	 * 
	 * @Description:TODO
	 * @param sEcho
	 * @return void
	 * @exception:
	 * @author: 
	 * @time:2014-12-18 上午10:37:42
	 */
	public void setsEcho(String sEcho) {
		this.sEcho = sEcho;
	}
	/**
	 * 
	 * @Description:TODO
	 * @return
	 * @return Long
	 * @exception:
	 * @author: 
	 * @time:2014-12-18 上午10:37:35
	 */
	public Long getiDisplayLength() {
		return iDisplayLength;
	}
	/**
	 * 
	 * @Description:TODO
	 * @param iDisplayLength
	 * @return void
	 * @exception:
	 * @author: 
	 * @time:2014-12-18 上午10:37:28
	 */
	public void setiDisplayLength(Long iDisplayLength) {
		this.iDisplayLength = iDisplayLength;
	}
	/**
	 * 
	 * @Description:TODO
	 * @return
	 * @return Long
	 * @exception:
	 * @author: 
	 * @time:2014-12-18 上午10:37:21
	 */
	public Long getiDisplayStart() {
		return iDisplayStart;
	}
	/**
	 * 
	 * @Description:TODO
	 * @param iDisplayStart
	 * @return void
	 * @exception:
	 * @author: 
	 * @time:2014-12-18 上午10:37:14
	 */
	public void setiDisplayStart(Long iDisplayStart) {
		this.iDisplayStart = iDisplayStart;
	}

	/**
	 * 
	 * @Title: getCurrentPage
	 * @Description: TODO 计算当前页码
	 * @author zhanglei
	 * @date 2014-11-18 下午6:01:45
	 * @param @return 设定文件
	 * @return Long 返回类型
	 * @throws
	 */
	public Long getCurrentPage() {
		if (getiDisplayLength() <= 0) {
			return 1L;
		}
		return getiDisplayStart() / getiDisplayLength() + 1L;
	}

	/**
	 * 
	 * @Title: combinTotalRecords
	 * @Description: TODO 拼装数据条数
	 * @author zhanglei
	 * @date 2014-11-18 下午6:02:03
	 * @param @param tList
	 * @param @return 设定文件
	 * @return DataTables<T> 返回类型
	 * @throws
	 */
	public static <T> DataTables<T> combinTotalRecords(List<T> tList) {
		DataTables<T> dTables = new DataTables<T>();
		PageList<T> pageList = (PageList<T>) tList;
		long totalCount = (long) pageList.getPaginator().getTotalCount();
		long startRow = (long) pageList.getPaginator().getStartRow();
		long endRow = (long) pageList.getPaginator().getEndRow();
		dTables.setiTotalRecords(totalCount);
		dTables.setiTotalDisplayRecords(totalCount);
		dTables.setiDisplayStart(startRow-1);
		dTables.setiDisplayLength(endRow-startRow+1);
		dTables.setAaData(tList);
		return dTables;
	}
	/**
	 * 
	* @Title: combinTotalRecordsNoPaginate 
	* @Description: TODO 无分页形式返回
	* @author zhanglei  
	* @date 2014-12-3 上午9:10:03 
	* @param @param tList
	* @param @return    设定文件 
	* @return DataTables<T>    返回类型 
	* @throws
	 */
	public static <T> DataTables<T> combinTotalRecordsNoPaginate(List<T> tList) {
		DataTables<T> dTables = new DataTables<T>();
		dTables.setAaData(tList);
		return dTables;
	}

	/**
	 * 
	 * @Title: getPageBounds
	 * @Description: TODO mybatis-paginator分页
	 * @author zhanglei
	 * @date 2014-11-19 下午3:19:07
	 * @param @param sortString
	 * @param @return 设定文件
	 * @return PageBounds 返回类型
	 * @throws
	 */
	public PageBounds getPageBounds(String sortString) {
		return new PageBounds(getCurrentPage().intValue(), getiDisplayLength().intValue(), Order.formString(sortString));
	}
	/**
	 * 
	* @Title: getPageBoundsNoOrder 
	* @Description: TODO mybatis-paginator分页 无指定排序字段
	* @author ZhangLei  
	* @date 2016年6月22日 上午11:32:44 
	* @param @return    设定文件 
	* @return PageBounds    返回类型 
	* @throws
	 */
	public PageBounds getPageBoundsNoOrder() {
		return new PageBounds(getCurrentPage().intValue(), getiDisplayLength().intValue());
	}
}
